package com.link.base.base.system.utils;

import com.link.base.base.system.model.TreeNode;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.service.snowflake.KeyGenerateUtil;
import com.link.core.util.BeanTools;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * 对表lnk_party_rel执行增删改的线程
 *
 * @author LiYang
 * @date 2019-12-13
 **/
public class PartyRelOperator implements Runnable {

    /**
     * 新增语句前缀
     */
    public static final String INSERT_PARTY_REL_PREFIX = "insert into lnk_party_rel "
            + "(row_id,created,created_by,last_upd,last_upd_by,party_code,party_name,"
            + "sub_party_id,sub_party_name,party_type,sub_party_code,party_id,corp_id,org_id,postn_id,data_type) values ";

    /**
     * 删除语句前缀
     */
    public static final String DELETE_PARTY_REL_PREFIX = "delete from lnk_party_rel where row_id = ";

    /**
     * 数据列表
     */
    private List<TreeNode> dataList;

    /**
     * 操作符
     */
    private String operator;

    /**
     * 数据类型
     */
    private String dataType;

    /**
     * 当前系统用户
     */
    private CoreUser user;

    /**
     * 构造函数
     *
     * @author LiYang
     * @date 2019/12/13
     * @param dataList 数据列表
     * @param operator 操作符，取值TreeUtil中的*_OPERATOR常量
     * @param dataType 区分组织、职位等
     */
    public PartyRelOperator(List<TreeNode> dataList, String operator, String dataType, CoreUser user) {
        this.dataList = dataList;
        this.operator = operator;
        this.dataType = dataType;
        this.user = user;
    }

    @Override
    public void run() {
        JdbcTemplate jdbcTemplate = (JdbcTemplate) BeanTools.getBean("jdbcTemplate");
        //往lnk_party_rel表中写入数据
        if (TreeUtil.INSERT_OPERATOR.equals(this.operator)) {
            //每1000个插入语句为一组
            int batch = dataList.size() / 1000;
            //计算列表结束为止
            int endIndex;
            for (int i = 0; i <= batch; i++) {
                //拼接insert语句，每次1000条
                StringBuilder sb = new StringBuilder(INSERT_PARTY_REL_PREFIX);
                if ((i + 1) * 1000 > dataList.size()) {
                    endIndex = dataList.size();
                } else {
                    endIndex = (i + 1) * 1000;
                }
                List<TreeNode> subList = this.dataList.subList(i * 1000, endIndex);
                for (TreeNode node : subList) {
                    sb.append("(");
                    sb.append("'" + KeyGenerateUtil.keyGenerate().toString() + "',");
                    sb.append("now()" + ",");
                    sb.append(this.user.getId() + ",");
                    sb.append("now()" + ",");
                    sb.append(this.user.getId() + ",");
                    sb.append("'" + node.getParentCode() + "'" + ",");
                    sb.append("'" + node.getParentName() + "'" + ",");
                    sb.append("'" + node.getId() + "'" + ",");
                    sb.append("'" + node.getName() + "'" + ",");
                    sb.append("'" + node.getOriginType() + "'" + ",");
                    sb.append("'" + node.getCode() + "'" + ",");
                    sb.append("'" + node.getParentId() + "'" + ",");
                    sb.append(this.user.getCorpid() + ",");
                    sb.append(this.user.getOrgId() + ",");
                    sb.append(this.user.getPostnId() + ",");
                    sb.append("'" + this.dataType + "')");
                    sb.append(",");
                }
                sb.deleteCharAt(sb.length() - 1);
                //执行新建
                jdbcTemplate.update(sb.toString());
            }
        }
        //往lnk_party_rel表中更新数据
        if (TreeUtil.UPDATE_OPERATOR.equals(this.operator)) {
            String[] updateSql = new String[this.dataList.size()];
            int i = 0;
            for (TreeNode node : this.dataList) {
                //拼接更新语句
                StringBuilder sb = new StringBuilder();
                sb.append("update lnk_party_rel set last_upd = now()");
                sb.append(",last_upd_by = " + user.getId());
                sb.append(",party_id = " + node.getParentId());
                sb.append(",party_code = '" + node.getParentCode() + "'");
                sb.append(",party_name = '" + node.getParentName() + "'");
                sb.append(",sub_party_id = " + node.getId());
                sb.append(",sub_party_code = '" + node.getCode() + "'");
                sb.append(",sub_party_name = '" + node.getName() + "'");
                sb.append(",party_type = '" + node.getOriginType() + "'");
                sb.append(",data_type = '" + this.dataType + "'");
                sb.append(" where row_id = '" + node.getRowId() + "'");
                updateSql[i] = sb.toString();
                i++;
            }
            //执行更新
            jdbcTemplate.batchUpdate(updateSql);
        }
        //往lnk_party_rel表中删除数据
        if (TreeUtil.DELETE_OPERATOR.equals(this.operator)) {
            String[] deleteSql = new String[this.dataList.size()];
            int i = 0;
            for (TreeNode node : this.dataList) {
                StringBuilder sb = new StringBuilder();
                //拼接删除语句
                sb.append(DELETE_PARTY_REL_PREFIX);
                sb.append("'");
                sb.append(node.getRowId());
                sb.append("'");
//                String deleteSql = DELETE_PARTY_REL_PREFIX + "'" + node.getRowId() + "'";
                deleteSql[i] = sb.toString();
                i++;
            }
            //执行删除
            jdbcTemplate.batchUpdate(deleteSql);
        }
    }
}
